package com.wnxy.web.servlet;

import com.wnxy.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/deleteEmp")
public class DeleteByIdServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");//处理接收POST请求的乱码
        resp.setContentType("text/html;charset=UTF-8");//解决响应到浏览器的内容的乱码问题
        //获取id
        String id = req.getParameter("id");
        if(id==null||id.equals("")||id.equals("0")){
            resp.getWriter().write("<script>alert('警告，你输入的编号有误')</script>");
            resp.setHeader("refresh","0,url="+req.getContextPath()+"/onePage");
            return;
        }
        EmployeeServiceImpl esi = new EmployeeServiceImpl();
        boolean flag = esi.deleteById(Integer.parseInt(id));
        if (flag){
            resp.getWriter().write("<script>alert('恭喜，删除成功')</script>");
            resp.setHeader("refresh","0,url="+req.getContextPath()+"/onePage");
        }else {
            resp.getWriter().write("<script>alert('警告，删除失败')</script>");
            resp.setHeader("refresh","0,url="+req.getContextPath()+"/onePage");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
